Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    value1:1,
    tabList: [
      {
        text:'全部',
        value:1,
      },
      {
        text: '关注动态',
        value:2,
      },
      {
        text: '系统通知',
        value:3,
      },
      {
        text: '风险监控',
        value:4,
      },
    ],
    timeList:[
      {name:'全部',value:1,active:true},
      {name:'近7天',value:2,active:false},
      {name:'近30天',value:3,active:false},
      {name:'近半年',value:4,active:false},
    ],
    currentClick:'start',
    startTime:'',
    endTime:'',
    currentDate: new Date().getTime(),
    minDate: new Date(2019,1, 1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    show:false,
    startsjc:0,
    endsjc:0,
  },
  toprodetail(item) {
    console.log(item, 'item')
    switch (item.detail.title) {
      case '银企对接':
        wx.navigateTo({
          url: '/pages/newsDetail/newsDetail'
        })
        break;
      case '审批记录':
        wx.navigateTo({
          url: '/sub_shenpi/shenpijilu/shenpijilu?type=1'
        })
        break;
      default:
        break;
    }
  },
  async getList() {
    // const res = await getApp().globalData.request(
    //   "/mes/query", {},
    //   "POST"
    // );
    console.log(res, 'res')
    const res = {
      data: [{
        title: '银企对接',
      },
      {
        title: '审批记录',
        count: 12
      }
    ]
    } 
    if (res.data.length > 0) {
      res.data.forEach(pre => {
        // 银企对接
        switch (pre.title) {
          case '银企对接':
            pre.title1 = '银企对接1'
            pre.title2 = '银企对接2'
            pre.newsTime = '2023-03-22'
            break;
          case '审批记录':
            pre.title1 = '待审核记录'
            pre.title2 = `您有${pre.count}条申请记录待审核`
            break;
          default:
            break;
        }
      })
      this.setData({
        list: res.data
      })
    }
    // console.log(res, 'res')
  },
  //点击筛选时间
  timeListTab(e){
    //时间选择框置空
    this.setData({
    startTime:'',
    endTime:'',
    currentDate: new Date().getTime(),
    minDate: new Date(2019,1, 1).getTime(),
    startsjc:0,
    endsjc:0,
    })
      const {value}=e.currentTarget.dataset;
      let arrA=this.data.timeList;
      arrA.map(item=>{
        if(item.value==value){
          item.active=true
        }
        else{
          item.active=false;
        }
      })
      this.setData({
        timeList:[...arrA]
      })
  },
  //日期选择器打开
  timePicker(e){
   const {value}=e.currentTarget.dataset;
   const {startsjc,endsjc}=this.data;
   let currentTime=new Date().getTime();
   this.setData({
     show:true,
     currentClick:value,
     currentDate:value=='start'?(startsjc?new Date(startsjc).getTime():currentTime):(endsjc?new Date(endsjc).getTime():currentTime)
   })
  },
  //日期选择
  onInput(event) {
    this.setData({
      currentDate:new Date(event.detail).getTime(),
    });
  },
  onConfirm(){
       this.setData({
          show:false,
          startTime:this.data.currentClick=='start'?this.timeChange(this.data.currentDate,'Y-m-d'):this.data.startTime,
          endTime:this.data.currentClick=='end'?this.timeChange(this.data.currentDate,'Y-m-d'):this.data.endTime,
          startsjc:this.data.currentClick=='start'?this.data.currentDate:this.data.startsjc, //开始时间戳
          endsjc:this.data.currentClick=='end'?this.data.currentDate:this.data.endsjc, //结束时间戳
        })
  },
  onClose() {
    this.setData({ show: false });
  },

  //转换时间
  timeChange (timestamp, formats) {
    // formats格式包括
    // 1. Y-m-d
    // 2. Y-m-d H:i:s
    // 3. Y年m月d日
    // 4. Y年m月d日 H时i分
    formats = formats || 'Y-m-d';

    let zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };

    let myDate = timestamp? new Date(timestamp): new Date();

    let year = myDate.getFullYear();
    let month = zero(myDate.getMonth() + 1);
    let day = zero(myDate.getDate());

    let hour = zero(myDate.getHours());
    let minite = zero(myDate.getMinutes());
    let second = zero(myDate.getSeconds());

    return formats.replace(/Y|m|d|H|i|s/ig, function (matches) {
        return ({
            Y: year,
            m: month,
            d: day,
            H: hour,
            i: minite,
            s: second
        })[matches];
    });
},
//点击搜索
timeSearh(){
   if( !this.data.startTime || !this.data.endTime){
        return wx.showModal({
          title: '提示',
          content: '请选择开始和结束日期',
          showCancel:false
        })
    }
     else  if(this.data.startsjc>=this.data.endsjc){
        //判断结束日期大于开始日期
          return wx.showModal({
            title: '提示',
            content: '开始日期不能大于等于结束日期',
            showCancel:false
          })
      }
      else{
        let arrA=this.data.timeList;
        arrA.map(item=>{
          item.active=false;
        });
        this.setData({
          show:false,
          timeList:[...arrA]
        })
        this.selectComponent('#moreSelect').toggle();
      }

},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})